package com.intelligentpension.fingerprintidentification

import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.*
import com.hjq.toast.ToastUtils
import com.orhanobut.logger.Logger
import javax.crypto.Cipher


/**
 * 参考文献 https://blog.csdn.net/wjky2014/article/details/119881017
 * 参考文献 https://blog.csdn.net/lintcgirl/article/details/51355203
 * 参考文献 https://blog.csdn.net/generallizhong/article/details/89147931
 *
 * 如果要手机指纹信息,而不是指纹验证 https://blog.csdn.net/luqingshuai_eloong/article/details/105438486
 */
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById<Button>(R.id.button).setOnClickListener {
            //官方的指纹识别在Android 6.0引入，Android 6.0之前指纹识别由厂商自己定义。
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                //在Android 9 版本和之前版本中会调用FingerprintManagerCompat
                //在Android 10 版本开始会调用框架层BiometricManager
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {

                    var fingerprintAvailable =
                        FingerUtils.isFingerprintAvailable10(this@MainActivity)

                    when (fingerprintAvailable) {
                        BiometricManager.BIOMETRIC_SUCCESS -> {

                            ToastUtils.show("支持蓝牙")

                            FingerUtils.authenticate10(this@MainActivity,
                                object : BiometricPrompt.AuthenticationCallback() {
                                    override fun onAuthenticationError(
                                        errorCode: Int, errString: CharSequence
                                    ) {
                                        super.onAuthenticationError(errorCode, errString)

                                        when (errorCode) {
                                            ERROR_USER_CANCELED -> ToastUtils.show("取消了指纹识别")
                                            ERROR_LOCKOUT -> ToastUtils.show("失败5次，已锁定，请30秒后在试")
                                            ERROR_LOCKOUT_PERMANENT -> {
                                                ToastUtils.show("失败次数太多，指纹验证已锁定，请改用密码，图案等方式解锁")
                                                ToastUtils.show("点击了negative button")
                                            }
                                            ERROR_NEGATIVE_BUTTON -> ToastUtils.show("点击了negative button")
                                            ERROR_NO_DEVICE_CREDENTIAL -> ToastUtils.show("尚未设置密码，图案等解锁方式")
                                            ERROR_NO_SPACE -> ToastUtils.show("可用空间不足")
                                            ERROR_TIMEOUT -> ToastUtils.show("验证超时")
                                        }

                                    }

                                    override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
                                        super.onAuthenticationSucceeded(result)

                                        Logger.e("result.authenticationType=="+result.authenticationType+"result.cryptoObject"+result.cryptoObject)
                                        ToastUtils.show("指纹验证成功")
                                    }

                                    override fun onAuthenticationFailed() {
                                        super.onAuthenticationFailed()
                                        ToastUtils.show("验证失败,请重试")
                                    }
                                })
                        }
                        BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
                            ToastUtils.show("不支持蓝牙")
                        }
                        else -> {
                            ToastUtils.show("不支持蓝牙")
                        }
                    }

                } else {

                    var fingerprintAvailable =
                        FingerUtils.isFingerprintAvailable6(this@MainActivity)
                    when (fingerprintAvailable) {

                        FingerUtils.Stage.UNSUPORTFINGER -> {
                            ToastUtils.show("您的手机不支持指纹功能")
                            return@setOnClickListener
                        }
                        FingerUtils.Stage.NEED_CLOSE_SCREAM_FINGER -> {
                            ToastUtils.show("您还未设置锁屏,请先设置锁屏并添加指纹")
                            return@setOnClickListener
                        }
                        FingerUtils.Stage.NEED_FINGER -> {
                            ToastUtils.show("您至少需要在系统设置中添加一个指纹")
                            return@setOnClickListener
                        }
                        else -> {
                            FingerUtils.authenticate6(
                                this@MainActivity,
                                object : FingerUtils.CipherListener {
                                    override fun backCipher(cipher: Cipher) {
                                        var fragment = FingerPrintDialogFragment()
                                        fragment.setCipher(cipher)
                                        fragment.show(supportFragmentManager, "fingerprint")
                                    }
                                })
                        }
                    }
                }
            } else {
                ToastUtils.show("咨询手机商家是否支持指纹识别")
            }
        }
    }


}